import sys
import os
import glob  

import frt
import frtUI

from PyQt4 import QtCore, QtGui, uic

from mc_Qt_ui import Ui_mainWindow_MC, Ui_widget_frt

ui_files = glob.glob('QtGUI/ui/*.ui')
if ui_files:
	file_name, ext = os.path.splitext(__file__)
	py_ui_fh = open(file_name + '_ui.py', 'w')
	
	for ui_file in ui_files:
		uic.compileUi(ui_file, py_ui_fh, execute=False, indent=0)
		
	py_ui_fh.close()

_connect_ = QtCore.QObject.connect
_signal_ = QtCore.SIGNAL

def start():
	app = QtGui.QApplication(sys.argv)
	ui = MC_Qt()
	ui.show()
	sys.exit(app.exec_())

class FRT_Qt(QtGui.QWidget, frtUI.FRTUserInterface):
	def __init__(self, Parent=None):
		QtGui.QWidget.__init__(self, Parent)
		self.ui = Ui_widget_frt()
		self.ui.setupUi(self)
		
		self._frt = frt.FRT(self)
		_connect_(self, _signal_("Directory Scan"), self._frt.directory_scan)
		_connect_(self, _signal_("Shows Changed"), self.update_shows)
		
		self._itemModel = QtGui.QStandardItemModel(1, 1)
		self.ui.treeView.setModel(self._itemModel)
		
		#self.emit(_signal_("Directory Scan"), "../test")
		
	def update_shows(self):
		shows = self._frt.get_shows()
		parentItem = self._itemModel.invisibleRootItem()
		
		for show in shows:
			showItem = QtGui.QStandardItem(show)
			parentItem.appendRow(showItem)
			seasons = shows[show].get_seasons()
			for season in seasons:
				seasonItem = QtGui.QStandardItem("Season " + str(season))
				showItem.appendRow(seasonItem)
				episodes = seasons[season].get_episodes()
				for episode in episodes:
					episodeItem = QtGui.QStandardItem("Episode " + str(episode))
					seasonItem.appendRow(episodeItem)
			
	@QtCore.pyqtSignature("")
	def on_pButton_browse_clicked(self):
		directory = QtGui.QFileDialog.getExistingDirectory(
			self, 
			"Open Directory", 
			__file__, 
			QtGui.QFileDialog.ShowDirsOnly)
		
		self.emit(_signal_("Directory Scan"), str(directory))
		
	''' Interface '''
	def shows_changed(self):
		self.emit(_signal_("Shows Changed"))
	
		

class MC_Qt(QtGui.QMainWindow):
	def __init__(self):
		QtGui.QWidget.__init__(self)
		self.ui = Ui_mainWindow_MC()
		self.ui.setupUi(self)
		
		# Add the Fantastic Remembering Thingy widget
		self._frt = FRT_Qt()
		
		self._mainLayout = QtGui.QVBoxLayout(self.ui.frame_central)
		self._mainLayout.addWidget(self._frt)